package com.shankungfu.baidu.push;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.baidu.android.pushservice.PushConstants;
import com.shankungfu.MainApplication;
import com.shankungfu.util.TestUtils;

/**
 * Push消息处理receiver
 */
public class PushMessageReceiver extends BroadcastReceiver {
	/** TAG to Log */
	public static final String TAG = PushMessageReceiver.class.getSimpleName();
	public static final String BAIDU_PUSH_URL = "URL";

	/**
	 * @param context
	 *            Context
	 * @param intent
	 *            接收的intent
	 */
	@Override
	public void onReceive(final Context context, Intent intent) {

		Log.d(TAG, ">>> Receive intent: \r\n" + intent);

		if (intent != null
				&& intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
			// 获取消息内容
			String message = intent.getExtras().getString(
					PushConstants.EXTRA_PUSH_MESSAGE_STRING);

			// 消息的用户自定义内容读取方式
			Log.i(TAG, "onMessage: " + message);

			// 自定义内容的json串
			Log.d(TAG,
					"EXTRA_EXTRA = "
							+ intent.getStringExtra(PushConstants.EXTRA_EXTRA));

		} else if (intent.getAction().equals(PushConstants.ACTION_RECEIVE)) {
			// 处理绑定等方法的返回数据
			// PushManager.startWork()的返回值通过PushConstants.METHOD_BIND得到

			// 可选。通知用户点击事件处理
		} else if (intent.getAction().equals(
				PushConstants.ACTION_RECEIVER_NOTIFICATION_CLICK)) {
			Log.d(TAG, "intent=" + intent.toUri(0));

			String content = intent
					.getStringExtra(PushConstants.EXTRA_NOTIFICATION_CONTENT);

			Intent aIntent = new Intent();
			aIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			ComponentName comp = new ComponentName(context.getPackageName(),
					context.getPackageName() + ".main.SplashScreenActivity");
			aIntent.setComponent(comp);
			aIntent.setAction("android.intent.action.MAIN");
			aIntent.addCategory("android.intent.category.LAUNCHER");
			context.startActivity(aIntent);

		}
	}
	
}
